package com.logistics.distribution;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.logistics.database.Distributiondatabase;

public class Newgoodsservlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	Distributiondatabase ddb;
	public Newgoodsservlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	       String type = request.getParameter("type");
	       String name1=new String(request.getParameter("name1").getBytes("iso-8859-1"),"utf-8");
	       if(name1==null) name1="";
	       String name2=new String(request.getParameter("name2").getBytes("iso-8859-1"),"utf-8");
	       if(name2==null) name2="";	
	       System.out.println("gettype="+type);  
	       ResultSet rs1=ddb.categoryLevel1Query();
	       ArrayList<String> category1 = new ArrayList<String>();
	       try {
	    	   while(rs1.next())
			   {
	    		   category1.add(rs1.getString(1));
			   }
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}     
		       

	       
	       ResultSet rs3=ddb.suppliersQuery();
	       ArrayList<String> suppliers = new ArrayList<String>();
	       try {
	    	   while(rs3.next())
			   {
				   suppliers.add(rs3.getString(1));
			   }
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	       

	       request.setAttribute("category1", category1);
	       request.setAttribute("supplier", suppliers);
	       request.setAttribute("selectoption1", name1);
	       request.setAttribute("selectoption2", name2);
	       
	       if(type.equals("delete"))
	       {
	    	   String id=request.getParameter("product_id");
	    	   ddb.productsDelete(id);
	       }

	       else if(type.equals("insert"))
	       {
	    	   String product_name=new String(request.getParameter("product_name").getBytes("iso-8859-1"),"utf-8");
	    	   String unit=new String(request.getParameter("product_unit").getBytes("iso-8859-1"),"utf-8");
	    	   String price=new String(request.getParameter("product_price").getBytes("iso-8859-1"),"utf-8");
	    	   String discount=new String(request.getParameter("product_discount").getBytes("iso-8859-1"),"utf-8");
	    	   String prime_cost=new String(request.getParameter("product_prime_cost").getBytes("iso-8859-1"),"utf-8");
	    	   String product_type=new String(request.getParameter("product_type").getBytes("iso-8859-1"),"utf-8");
	    	   String supplier_name=new String(request.getParameter("product_supplier").getBytes("iso-8859-1"),"utf-8");
	    	   String producer=new String(request.getParameter("product_producer").getBytes("iso-8859-1"),"utf-8");
	    	   String qg_period=new String(request.getParameter("product_qg_period").getBytes("iso-8859-1"),"utf-8");
	    	   String tips=new String(request.getParameter("product_tips").getBytes("iso-8859-1"),"utf-8");
	    	   String can_return=new String(request.getParameter("can_return").getBytes("iso-8859-1"),"utf-8");
	    	   String warning_number=new String(request.getParameter("product_warning").getBytes("iso-8859-1"),"utf-8");
	    	   String max_number=new String(request.getParameter("product_max").getBytes("iso-8859-1"),"utf-8");
	    	   String delete_id=new String(request.getParameter("delete_id").getBytes("iso-8859-1"),"utf-8");
	    	   
	    	   if(delete_id.equals("new")) 
	    	   {
	    		   System.out.println("insert new product");
	    		   ddb.productsInsert(product_name, name1, name2, unit, price, discount, prime_cost, product_type, supplier_name, producer, qg_period, tips, can_return, warning_number, max_number);
	    	   }
	    	   else
	    	   {
	    		   System.out.println("update product");
	    		   ddb.productsUpdate(delete_id,product_name, name1, name2, unit, price, discount, prime_cost, product_type, supplier_name, producer, qg_period, tips, can_return, warning_number, max_number);
	    	   }

	       }else if(type.equals("update"))
	       {
	    	   String id=request.getParameter("product_id");
	    	   ResultSet temprs=ddb.productsQuery(Integer.parseInt(id));
	    	   Product pd=new Product();
			   try {
				   if(temprs.next())
				   {
					   pd.setProduct_id(temprs.getString(1));
					   pd.setProduct_name(temprs.getString(2));
					   pd.setCategory_lv1(temprs.getString(3));
					   pd.setCategory_lv2(temprs.getString(4));
					   pd.setUnit(temprs.getString(5));
					   pd.setPrice(temprs.getString(6));
					   pd.setDiscount(temprs.getString(7));
					   pd.setPrime_cost(temprs.getString(8));
					   pd.setType(temprs.getString(9));
					   pd.setSupplier_name(temprs.getString(10));
					   pd.setProducer(temprs.getString(11));
					   pd.setQg_period(temprs.getString(12));
					   pd.setTips(temprs.getString(13));
					   pd.setCan_return(temprs.getString(14));
					   pd.setWarning_number(temprs.getString(15));
					   pd.setMax_number(temprs.getString(16));
				   }
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			   name1=pd.getCategory_lv1();
			   name2=pd.getCategory_lv2();
			   request.setAttribute("updateproduct", pd);
		       request.setAttribute("selectoption1", pd.getCategory_lv1());
		       request.setAttribute("selectoption2", pd.getCategory_lv2());
		       request.setAttribute("selectoption3", pd.getSupplier_name());
	       }
	       
	       ResultSet rs2=ddb.categoryLevel2Query(name1);
	       ArrayList<String> category2 = new ArrayList<String>();
	       try {
	    	   while(rs2.next())
			   {
				   category2.add(rs2.getString(2));
			   }
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	       
	       ResultSet rs4;
	       if(name1.length()==0)
	       {
	    	   rs4=ddb.productsQuery();
	       }
	       else if(name2.length()==0)
	       {
	    	   rs4=ddb.productsQuery(name1);
	       }
	       else
	       {
	    	   rs4=ddb.productsQuery(name1, name2);
	       }
	       ArrayList<Product> products=new ArrayList<Product>();
	       try {
	    	   while(rs4.next())
			   {
				   Product pd=new Product();
				   pd.setProduct_id(rs4.getString(1));
				   pd.setProduct_name(rs4.getString(2));
				   pd.setCategory_lv1(rs4.getString(3));
				   pd.setCategory_lv2(rs4.getString(4));
				   pd.setUnit(rs4.getString(5));
				   pd.setPrice(rs4.getString(6));
				   pd.setDiscount(rs4.getString(7));
				   pd.setPrime_cost(rs4.getString(8));
				   pd.setType(rs4.getString(9));
				   pd.setSupplier_name(rs4.getString(10));
				   pd.setProducer(rs4.getString(11));
				   pd.setQg_period(rs4.getString(12));
				   pd.setTips(rs4.getString(13));
				   pd.setCan_return(rs4.getString(14));
				   pd.setWarning_number(rs4.getString(15));
				   pd.setMax_number(rs4.getString(16));
				   System.out.println(pd.toString());
				   products.add(pd);
			   }
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	       request.setAttribute("products", products);
	       request.setAttribute("category2", category2);
	       
	       if(type.equals("newgoods")||type.equals("update"))
	       {
	    	   System.out.println("arrive here");
	    	   request.getRequestDispatcher("../distribution/Newgoods.jsp").forward(request, response); 
	       }
	       else 
	       {
		       request.getRequestDispatcher("../distribution/Querygoods.jsp").forward(request, response);  
	       }
  	
 
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request,response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
		ddb=new Distributiondatabase();
	}

}
